package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.listener.OrderSettingListener;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderSettingService;
import cn.itcast.vo.OrderSettingVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;

//预约日历
@RestController
@RequestMapping(path = "/ordersetting")
@CrossOrigin
public class OrderSettingController {

    @Reference(version = "1.0.0")
    private OrderSettingService orderSettingService;




    /**
     * 查询当月的预约数据
     * @return
     */
    @GetMapping( "/getOrderSettingsByMonth")
    public Result findAll(String date) {
        //分页查询预约信息
        try {
            List<OrderSettingVo> orderSettingVos = orderSettingService.findAll(date);
                return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, orderSettingVos);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 更改可预约人数
     * @return
     */
    @PutMapping( "/updateOrderSettingNumber")
    public Result update(@RequestBody Map<String,Object> params) {
        try {
            String orderDate = (String) params.get("orderDate");
            String number = (String) params.get("number");

            String update = orderSettingService.update(orderDate, Integer.valueOf(number));

            if("请合理设置预约人数".equals(update)){
                return new Result(false, MessageConstant.ORDERSETTINGS_FAIL);
            }
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ORDERSETTING_FAIL);
        }
    }


    /**
     * 读取文件的内容 批量或者修改预约人数
     * @param excelFile
     * @return
     */
    @PostMapping("upload")
    public Result upLoad(MultipartFile excelFile){
        try {

            EasyExcel.read(excelFile.getInputStream(), OrderSetting.class, new OrderSettingListener(orderSettingService)).sheet().doRead();
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }








}
